const mixins = require('../../mixins/forum')
const common = require('../../mixins/common')
const app = getApp()
import config from "../../api/config";
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    navbarTrans: 0,
    color: "0,0,0",
    iconTheme: "white",
    showmenu: false,
    scrollTop: 0,
    page: 1,
    tabData: ["最新动态", "热门动态"],
    swithBarIndex: 0,
    posts: [],
    sticky: [],
    isLastPage: false,
    stickypage:1,
    isStickeyLastPage:false,
    memberList:[],
    mempage:1,
    circleloading:true,
    videoindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    })
    this.getCircleByIDs(options.id);
    this.getTopicLists({
      circle: options.id,
      page: this.data.page
    })
    this.getMenmberList(options.id,{page: this.data.mempage});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  
  onPageScroll: function onPageScroll(e) {
    var that = this;
    var trans = (e.scrollTop > 55 ? 55 : e.scrollTop) / 55;
    var frontColor = "#ffffff";
    var backgroundColor = "#000000";
    var iconTheme = "white";
    if (trans == 0) {
        frontColor = "#ffffff";
        backgroundColor = "#000000";
        iconTheme = "white";
    } else if (trans >= .4) {
        frontColor = "#000000";
        backgroundColor = "#ffffff";
        iconTheme = "black";
    }
    that.setData({
        navbarTrans: trans,
        iconTheme: iconTheme
    });
    wx.setNavigationBarColor({
        frontColor: frontColor,
        backgroundColor: frontColor,
        animation: {
            duration: 400,
            timingFunc: "easeIn"
        }
    });
    
    that.data.scrollTop = e.scrollTop;
},

  
  /**
   *  跳转详情页
   */

  gotoDetail: function (e) {
    console.log(e);
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id+'&type=forums',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user = app.globalData.user
    if (!user) {
      user = '';
    }
    this.setData({
      user: user,
    })
  },

  /**跳转用户详情 */
  membersDidClick:function(e){
   wx.navigateTo({
     url: '/pages/circle/members?id='+this.data.id,
   })
  },


  /** 切换菜单 */

  onHandleClick: function (e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      topicload:true,
      topicNodate:false,
      swithBarIndex: index,
      page: 1,
      posts: [],
      sticky: [],
      isLastPage: false,
      isStickeyLastPage:false,
      stickypage:1,
    })
    if (index == 0) {
      this.getTopicLists({
        circle: this.data.id,
        page: this.data.page
      })
    } else {
      this.getStickyLists({
        circle: this.data.id,
        page: this.data.stickypage
      })
    }
  },


  /**跳转发布按钮 */
  
  gotoPost: function (e) {
    wx.navigateTo({
      url: '/pages/creat/creat?topic=' + this.data.category + '&topicid=' + this.data.id,
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      topicload:true,
      topicNodate:false,
      stickyNodate:false,
      page: 1,
      posts: [],
      sticky: [],
      isLastPage: false,
      isStickeyLastPage:false,
      stickypage:1,
    })
    if (this.data.swithBarIndex == 0) {
      this.getTopicLists({
        circle: this.data.id,
        page: this.data.page
      })
    } else {
      this.getStickyLists({
        circle: this.data.id,
        page: this.data.stickypage
      })
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isBottom: true,
    })
    if (!this.data.isLastPage && this.data.swithBarIndex == 0) {
      this.getTopicLists({
        circle: this.data.id,
        page: this.data.page,
      })
    } else if(!this.data.isStickeyLastPage && this.data.swithBarIndex == 1){
      this.getStickyLists({
        circle: this.data.id,
        page: this.data.stickypage
      })

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: this.data.category,
      path: '/pages/circle/list?id='+this.data.id,
      imageUrl: this.data.cover,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  },
  onShareTimeline: function (res) {
    return {
      title: this.data.category,
      query: 'id='+this.data.id,
      imageUrl:this.data.cover //图片地址
    }
  }
}

mixins(options)
common(options)
Page(options)